﻿
using Dting;
using Dting_libraries;
using EasyModbus;
using MaterialSkin;
using MaterialSkin.Controls;
using Station_interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_manage;
using static User_manage.User_Lgon;
using Timer = System.Windows.Forms.Timer;

namespace FormsData
{
    public partial class FormMain : MaterialForm
    {
        public bool logstate = false;
        bool closestae = false;
        ini_Class _ini_Class = new ini_Class("Config\\sys_data.ini");

        string[] MBar_data = new string[6];
        string[] modelinfo_data = new string[6];
        bool[] check_buf = new bool[8];
        int int_ProductionCount=0;

        public FormMain()
        {           
            InitializeComponent();

            //初始化窗体
            var materialSkinManager = MaterialSkinManager.Instance;
            materialSkinManager.EnforceBackcolorOnAllComponents = false;
            materialSkinManager.AddFormToManage(this);
            materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
            materialSkinManager.ColorScheme = new ColorScheme(
                       Primary.Blue900,
                       Primary.Blue900,
                       Primary.Blue900,
                       Accent.Blue200,
                       TextShade.WHITE);

            this.WindowState = FormWindowState.Maximized;

            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();

            //获取设备参数并初始化设备
            if (equipment_management1._DataTable==null && equipment_management1._DataTable.Rows.Count <=0)
            {
                MessageBox.Show("检测到无设备信息，软件无法正常执行。请添加设备信息后再重新启动软件！", "提示");
                Program._LogicClass.Info("初始化设备失败");
            }

            formlog = new Formlog();

            //初始化log打印
            Program._LogicClass = new logicClass(this.listBox1);
            //连接数据库
            Program._mySql_Class = new mySql_class("127.0.0.1", "3306", "root", "root.123", "droduce_data");
            //初始化PLC连接
            Program._PLC = new ModbusClient[3] { new ModbusClient(), new ModbusClient(), new ModbusClient() };

            //初始化添加控件
            equipment1._logicClass = Program._LogicClass;
            equipment1._mySql_class = Program._mySql_Class;
            equipment1._PLC = Program._PLC[0];
            equipment1.Station_init();


            //更新报警信息到每个工位
            Update_Aliarm();

            //
            //seek_adviceFrom1.Seek_mySql_Class = Station_Interface.Table_mySql_Class;


            //机型配置管理事件
            model_Management1._modelWrite_Click += Model_Management1__modelWrite_Click1;

            //二维条码测试写入PLC
            model_Management1._writeBarTest_Click += Model_Management1__writeBarTest_Click;

            //获取一次二维码参数
            string data_bar = _ini_Class.Read_Ini("sys_data", "Bar_name");
            DataRow[] mBardrArr = model_Management1._DataTable.Select("名称='" + data_bar + "'");   //查询
            if (mBardrArr != null && mBardrArr.Length >0)
            {
                MBar_data = mBardrArr[0].ItemArray.Select(i => i.ToString()).ToArray();
            }

            //获取产品方向配置型号
            string Model_name = _ini_Class.Read_Ini("sys_data", "Model_name");
            string subModel_name = _ini_Class.Read_Ini("sys_data", "sub_Model_name");
            List<Model_management.Model_Management.ModelManagement> _modelManagements = model_Management1.modelManagements;
            var buf = _modelManagements.Find(x => x._className == Model_name);
            if (buf != null)
            {
                DataRow[] BardrArr = buf._subClass._clasData.Select("配方ID='" + subModel_name + "'");   //查询
                if (BardrArr != null && BardrArr.Length > 0)
                {
                    modelinfo_data  = BardrArr[0].ItemArray.Select(i => i.ToString()).ToArray();
                    materialLabel6.Text = "产品配置：" + modelinfo_data[2];
                    materialLabel8.Text = "产品型号：" + modelinfo_data[1];
                    materialLabel9.Text = "产品方向：" + modelinfo_data[3];
                }
            }
            ;
            //获取检验状态
            for (int i = 0; i < 8; i++)
            {
                string str = "station_" + (i + 1).ToString();
                bool.TryParse(_ini_Class.Read_Ini("sys_data", str), out check_buf[i]);
                (groupBox2.Controls[str] as CheckBox).Checked = check_buf[i];
            }
            //获取计划产量
            int.TryParse(_ini_Class.Read_Ini("sys_data", "ProductionCount"), out int_ProductionCount);
            materialLabel3.Text = "计划产量：" + int_ProductionCount.ToString();

            //任务
            Task Alarm_task = new Task(run_task);
            //Alarm_task.Start();

            Program._LogicClass.Info("软件开启完成。。。");

            ;
            //userDataGridview1.UserDataGridview_Add("P1_工位", "H1230801005C", "NG");
            //userDataGridview1.UserDataGridview_Add("P1_工位", "H1230801005C", "NG");

            
        }

        //二维条码测试写入PLC
        private void Model_Management1__writeBarTest_Click(object sender)
        {
            string[] str_buf = sender as string[];
            //if (str_buf != null)
            //{
            //    if (station_Interfaces[7].Station._PLC != null && station_Interfaces[7].StationPLC._IsConnected)
            //    {
            //        station_Interfaces[7].Station._PLC.Write(DataType.Memory, 0, 7300, (string)" ");
            //        station_Interfaces[7].Station._PLC.Write(DataType.Memory, 0, 7310, (string)"    ");
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7320, (string)"     ");
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7330, (string)" ");
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7340, (string)"  ");

            //        //规格校验位
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7300, (string)str_buf[1]);
            //        //零件简号                    
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7310, (string)str_buf[2]);
            //        //供应商代码
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7320, (string)str_buf[3]);
            //        //生产线代号
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7330, (string)str_buf[4]);
            //        //软件版本代号
            //        station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7340, (string)str_buf[5]);

            //        Program._LogicClass.Info("测试二维码信息写入PLC完成。。。");
            //        MessageBox.Show("测试二维码信息写入PLC完成", "提示");
            //    }
            //    else
            //    {
            //        Program._LogicClass.Info("P8_工位PLC未联机，测试二维码信息写入PLC失败。。。");
            //        MessageBox.Show("P8_工位PLC未联机，测试二维码信息写入PLC失败", "提示");
            //    }
            //}
        }

        //机型配置管理事件
        private void Model_Management1__modelWrite_Click1(object sender, object dataname, object ModelName, object data, object Bar_data ,object model_data)
        {
            ;
            try
            {
                bool[] _sender = sender as bool[];
                string[] _dataname = dataname as string[];
                string[] _ModelName = ModelName as string[];
                string[] _data = data as string[];
                string[] _Bar_data = Bar_data as string[];
                string[] _model_data = model_data as string[];

                //缓存二维码数据
                MBar_data = _Bar_data;

                //写入PLC前检查状态
                bool Plc_Runstate = false; string runstate_info = "";
                bool Plc_Connstate = false; string conntate_info = "";
                for (int i = 0; i < station_Interfaces.Count; i++)
                {
                    if (_sender[i])
                    {
                        //if (station_Interfaces[i].StationPLC._PLC != null && station_Interfaces[i].StationPLC._IsConnected)
                        //{ Plc_Connstate = true; }
                        //else
                        //{ Plc_Connstate = false; conntate_info += "P" + (i + 1).ToString() + "_工位|"; }
                        //if (station_Interfaces[i].StationPLC.device_isWait)
                        //{ Plc_Runstate = true; }
                        //else
                        //{ Plc_Runstate = false; runstate_info += "P" + (i + 1).ToString() + "_工位|"; }
                    }

                }
                if (!Plc_Connstate)
                {
                    MessageBox.Show("设备未联机" + conntate_info, "提示");
                    return;
                }
                if (!Plc_Runstate)
                {
                    MessageBox.Show("设备运行中" + runstate_info, "提示");
                    return;
                }
                //写入PLC
                Task _Task1 = null;
                Task _Task2 = null;
                Task _Task3 = null;
                Task _Task4 = null;
                Task _Task5 = null;
                Task _Task6 = null;
                Task _Task7 = null;
                Task _Task8 = null;
                bool[] task_state = new bool[8] { false, false, false, false, false, false, false, false};
                bool[] C_state = new bool[8] { true, true, true, true, true, true, true, true };
                //CancellationTokenSource Cancellation = new CancellationTokenSource();
                ////设置超时的时间
                //Cancellation.CancelAfter(TimeSpan.FromSeconds(3));

                #region
                //#1
                if (_sender[0])
                {
                    _Task1 = Task.Run(() =>
                    {
                        bool state = true;
                        int log = 0;
                        int count = 0;

                        //station_Interfaces[0].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state)
                        {
                            //byte[] _value = station_Interfaces[0].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    //int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[0].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    //int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[0].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);

                                    ////产品方向
                                    //station_Interfaces[0].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    ////产品型号
                                    //station_Interfaces[0].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    ////产品配置
                                    //station_Interfaces[0].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[0].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log = 1;
                                    Program._LogicClass.Info("P1_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{

                                    //    //station_Interfaces[0].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log = 0; state = false;
                                    //    Program._LogicClass.Info("P1_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count++;
                            if (count >= 30)
                            {
                                //station_Interfaces[0].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state = false; C_state[0] = false;
                                string str_er = "P1_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[0] = true;
                        ;
                    });
                }
                else
                {
                    task_state[0] = true;
                }

                //#2
                if (_sender[1])
                {
                    _Task2 = Task.Run(() =>
                    {
                        bool state2 = true;
                        int log2 = 0;
                        int count2 = 0;
                        //station_Interfaces[1].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state2)
                        {
                            //byte[] _value = station_Interfaces[1].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log2)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    //int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[1].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    //int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[1].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    ////产品方向
                                    //station_Interfaces[1].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    ////产品型号
                                    //station_Interfaces[1].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    ////产品配置
                                    //station_Interfaces[1].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[1].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log2 = 1;
                                    Program._LogicClass.Info("P2_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{

                                    //    //station_Interfaces[1].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log2 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log2 = 0; state2 = false;
                                    //    Program._LogicClass.Info("P2_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count2++;
                            if (count2 >= 30)
                            {
                                //station_Interfaces[1].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state2 = false; C_state[1] = false;
                                string str_er = "P2_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[1] = true;
                    });
                }
                else
                {
                    task_state[1] = true;
                }
                //#3
                if (_sender[2])
                {
                    _Task3 = Task.Run(() =>
                    {
                        bool state3 = true;
                        int log3 = 0;
                        int count3 = 0;
                        //station_Interfaces[2].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state3)
                        {
                            //byte[] _value = station_Interfaces[2].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log3)
                            {
                                case 0:
                                    //写入配方ID
                                    //int vlaue = 0;
                                    //int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[2].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    //int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[2].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    ////产品方向
                                    //station_Interfaces[2].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    ////产品型号
                                    //station_Interfaces[2].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    ////产品配置
                                    //station_Interfaces[2].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[2].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log3 = 1;
                                    Program._LogicClass.Info("P3_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{
                                    //    //station_Interfaces[2].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log3 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log3 = 0;
                                    //    state3 = false;
                                    //    Program._LogicClass.Info("P3_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count3++;
                            if (count3 >= 30)
                            {
                                //station_Interfaces[2].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state3 = false; C_state[2] = false;
                                string str_er = "P3_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[2] = true;
                    });
                }
                else
                {
                    task_state[2] = true;
                }
                //#4
                if (_sender[3])
                {
                    _Task4 = Task.Run(() =>
                    {
                        bool state4 = true;
                        int log4 = 0;
                        int count4 = 0;
                        //station_Interfaces[3].Station._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state4)
                        {
                            //byte[] _value = station_Interfaces[3].Station._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log4)
                            {
                                case 0:
                                    //写入配方ID
                                    //int vlaue = 0;
                                    //int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[3].Station._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    //int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[3].Station._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    ////产品方向
                                    //station_Interfaces[3].Station._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    ////产品型号
                                    //station_Interfaces[3].Station._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    ////产品配置
                                    //station_Interfaces[3].Station._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[3].Station._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log4 = 1;
                                    Program._LogicClass.Info("P4_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{

                                    //    //station_Interfaces[3].Station._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log4 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log4 = 0; state4 = false;
                                    //    Program._LogicClass.Info("P4_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count4++;
                            if (count4 >= 30)
                            {
                                //station_Interfaces[3].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                task_state[0] = true; state4 = false; C_state[3] = false;
                                string str_er = "P4_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[3] = true;
                    });
                }
                else
                {
                    task_state[3] = true;
                }
                //#5
                if (_sender[4])
                {
                    _Task5 = Task.Run(() =>
                    {
                        bool state5 = true;
                        int log5 = 0;
                        int count5 = 0;
                        //station_Interfaces[4].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state5)
                        {
                            //byte[] _value = station_Interfaces[4].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log5)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[4].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    //int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[4].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    ////产品方向
                                    //station_Interfaces[4].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    ////产品型号
                                    //station_Interfaces[4].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    ////产品配置
                                    //station_Interfaces[4].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[4].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log5 = 1;
                                    Program._LogicClass.Info("P5_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{
                                    //    //station_Interfaces[4].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log5 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log5 = 0; state5 = false;
                                    //    Program._LogicClass.Info("P5_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count5++;
                            if (count5 >= 30)
                            {
                                //station_Interfaces[4].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state5 = false; C_state[4] = false;
                                string str_er = "P5_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[4] = true;
                    });
                }
                else
                {
                    task_state[4] = true;
                }
                //#6
                if (_sender[5])
                {
                    _Task6 = Task.Run(() =>
                    {
                        bool state6 = true;
                        int log6 = 0;
                        int count6 = 0;
                        //station_Interfaces[5].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state6)
                        {
                            //byte[] _value = station_Interfaces[5].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log6)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[5].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[5].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    //产品方向
                                    //station_Interfaces[5].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    //产品型号
                                    //station_Interfaces[5].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    //产品配置
                                    //station_Interfaces[5].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[5].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log6 = 1;
                                    Program._LogicClass.Info("P6_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{

                                    //    //station_Interfaces[5].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log6 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log6 = 0; state6 = false;
                                    //    Program._LogicClass.Info("P6_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count6++;
                            if (count6 >= 30)
                            {
                                //station_Interfaces[5].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state6 = false; C_state[5] = false;
                                string str_er = "P6_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[5] = true;
                    });
                }
                else
                {
                    task_state[5] = true;
                }
                //#7
                if (_sender[6])
                {
                    _Task7 = Task.Run(() =>
                    {
                        bool state7 = true;
                        int log7 = 0;
                        int count7 = 0;
                        //station_Interfaces[6].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state7)
                        {
                            //byte[] _value = station_Interfaces[6].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log7)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[6].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[6].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);


                                    //产品方向
                                    //station_Interfaces[6].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    //产品型号
                                    //station_Interfaces[6].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    //产品配置
                                    //station_Interfaces[6].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);

                                    //station_Interfaces[6].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log7 = 1;
                                    Program._LogicClass.Info("P7_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{

                                    //    //station_Interfaces[6].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log7 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log7 = 0; state7 = false;
                                    //    Program._LogicClass.Info("P7_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count7++;
                            if (count7 >= 30)
                            {
                                //station_Interfaces[6].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state7 = false; C_state[6] = false;
                                string str_er = "P7_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[6] = true;
                    });
                }
                else
                {
                    task_state[6] = true;
                }
                //#8
                if (_sender[7])
                {
                    _Task8 = Task.Run(() =>
                    {
                        bool state8 = true;
                        int log8 = 0;
                        int count8 = 0;
                        //station_Interfaces[7].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                        while (state8)
                        {
                            //byte[] _value = station_Interfaces[7].StationPLC._PLC.ReadBytes(DataType.Memory, 0, 5005, 1);

                            //bool Is_Produce = Convert.ToBoolean(_value[0] & (1 << 0)) ? true : false;//M5005.0
                            switch (log8)
                            {
                                case 0:
                                    //写入配方ID
                                    int vlaue = 0;
                                    int.TryParse(_ModelName[1], out vlaue);
                                    //station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7200, (ushort)vlaue);
                                    int.TryParse(_ModelName[2], out vlaue);
                                    //station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7202, (ushort)vlaue);

                                    //产品方向
                                    //station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7100, (string)_data[3]);
                                    //产品型号
                                    //station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7140, (string)_data[1]);
                                    //产品配置
                                    //station_Interfaces[7].StationPLC._PLC.Write(DataType.Memory, 0, 7180, (string)_data[2]);
                               
                                    //station_Interfaces[7].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, true);//m7005.0
                                    log8 = 1;
                                    Program._LogicClass.Info("P8_工位配置开始写入。");
                                    break;
                                case 1:
                                    //if (Is_Produce)//等待M5005.0
                                    //{
                                    //    //station_Interfaces[7].StationPLC._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                    //    log8 = 2;
                                    //}
                                    break;
                                case 2:
                                    //if (!Is_Produce)//等待M5005.0
                                    //{
                                    //    log8 = 0; state8 = false;
                                    //    Program._LogicClass.Info("P8_工位配置开始写入完成。");
                                    //}
                                    break;
                            }
                            Thread.Sleep(100);
                            count8++;
                            if (count8 >= 30)
                            {
                                //station_Interfaces[7].Station._PLC.WriteBit(DataType.Memory, 0, 7005, 0, false);//m7005.0
                                state8 = false; C_state[7] = false;
                                string str_er = "P8_工位配置写入失败，超时";
                                Program._LogicClass.Info(str_er);
                                MessageBox.Show(str_er, "提示");
                            }
                        }
                        task_state[7] = true;
                    });
                }
                else
                {
                    task_state[7] = true;
                }

                #endregion

                int a_count = 0;
                bool ta_state = true;
                while(ta_state)
                {
                    if (task_state[0] && task_state[1] && task_state[2] && task_state[3] && task_state[4] && task_state[5] && task_state[6] && task_state[7])
                    {
                        if (C_state[0] && C_state[1] && C_state[2] && C_state[3] && C_state[4] && C_state[5] && C_state[6] && C_state[7])
                        {
                            a_count = 0;
                            ta_state = false;
                        }
                        else
                        {
                            a_count = 0;
                            ta_state = false;
                            return;
                        }
                    }
                    a_count++;
                    if (a_count >=70)
                    {
                        a_count = 0;
                        ta_state = false;
                        return;
                    }
                    Thread.Sleep(100);
                }

                _ini_Class.Write_Ini("sys_data", "Model_name", _ModelName[0]);
                _ini_Class.Write_Ini("sys_data", "sub_Model_name", _ModelName[2]);
                _ini_Class.Write_Ini("sys_data", "Print_name", _dataname[1]);
                _ini_Class.Write_Ini("sys_data", "Bar_name", _dataname[2]);

                _ini_Class.Write_Ini("sys_data", "产品配置", _model_data[2]);
                _ini_Class.Write_Ini("sys_data", "产品型号", _model_data[1]);
                _ini_Class.Write_Ini("sys_data", "产品方向", _model_data[3]);

                this.SafeInvoke(() =>
                {
                    model_Management1.label1.Text = "当前使用机型类名：" + _dataname[0];
                    model_Management1.label6.Text = "当前使用名称：" + _dataname[2];

                });

                materialLabel6.Text = "产品配置：" + _model_data[2];
                materialLabel8.Text = "产品型号：" + _model_data[1];
                materialLabel9.Text = "产品方向：" + _model_data[3];

                MessageBox.Show("配置写入完成。。。", "提示");
            }
            catch (Exception er)
            {
                MessageBox.Show("配置写入失败，异常报错：" + er.ToString(), "提示");
                return;
            }
            ;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            _Timer1.Interval = 500;
            _Timer1.Tick += _Timer1_Tick; ;
            _Timer1.Start();

            Buf__BARstate_Click(this);
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closestae) return;

            //if (false)
            //{ MessageBox.Show("正在运行！请点击停止后才能关闭软件！", "提示"); e.Cancel = true; return; }

            if (MessageBox.Show("是否退出软件！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            { e.Cancel = false; closestae = true; }
            else
            { e.Cancel = true; }

            
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            _Timer.Stop();
            _Timer1.Stop();
            Program._LogicClass.Info("软件退出。。。");
            Program._LogicClass.Info("");
            End_form();
        }

        private void materialTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.materialTabControl1.SelectedIndex)
            {
                case 0:
                    if (User_Lgon.User_Level !=  H_Level.None) materialLabel1.Text = "用户已登录   " + User_Lgon.User_LevelStr;
                    else materialLabel1.Text = "用户未登录";

                    //
                    for (int i = 0; i < 8; i++)
                    {
                        string str = "station_" + (i + 1).ToString();
                        bool.TryParse(_ini_Class.Read_Ini("sys_data", str), out check_buf[i]);
                        (groupBox2.Controls[str] as CheckBox).Checked = check_buf[i];
                    }

                    //获取计划产量
                    int.TryParse(_ini_Class.Read_Ini("sys_data", "ProductionCount"), out int_ProductionCount);
                    materialLabel3.Text = "计划产量："+int_ProductionCount.ToString();
                    break;
                case 1:
                    model_Management1.get_PrintName();
                    model_Management1.User_sw();
                    break;
                case 2:
                    //printing_Interface1.User_sw(); logstate = false;
                    break;
                case 5:
                    logstate = false; User_Lgon.logstate = true;
                    break;

            }


        }

        //设备管理重连设备事件
        private void equipment_management1__Reconnect_Click(object sender, EventArgs e)
        {
            ;
            //string Station_na = (string)sender;
            //if (Station_na==string.Empty)
            //{
            //    MessageBox.Show("未选择机型", "提示");
            //}else
            //{
            //    if(main_LogicClass._ConnectPlc(Station_na)!=0)
            //    {
            //        MessageBox.Show("工位:" + Station_na + " 连接异常，或未找到指定设备", "提示");
            //    }
            //}
        }


        /***************************************************************************************************/
        //用户界面编辑完成后退出到主界面
        public Timer _Timer = new Timer();
        public Timer _Timer1 = new Timer();
        private void User_manageForm__FormClosed(object sender, FormClosedEventArgs e)
        {
            _Timer.Interval = 100;
            _Timer.Tick += _Timer_Tick;
            _Timer.Start();
        }

        private void _Timer_Tick(object sender, EventArgs e)
        {
            materialTabControl1.SelectedIndex = 0;
            _Timer.Stop();
        }
        private void _Timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (station_Interfaces != null && station_Interfaces.Count >= 7)
                {
                    int data = 0;
                    int.TryParse(station_Interfaces[7].uiTextBox3.Text.Trim(), out data);
                    materialLabel2.Text = "实际产量：" + data.ToString();

                    //Station_Interface.User_Level = User_Lgon.User_Level;
                    equipment_management1.User_Level = User_Lgon.User_Level;
                    model_Management1.User_Level = User_Lgon.User_Level;
                    //printing_Interface1.User_Level = User_Lgon.User_Level;
                    alarmControl1.User_Level = User_Lgon.User_Level;
                }
            }
            catch (Exception)
            {
                ;
            }      
        }

        //初始化工位界面
        List<Station_Interface> station_Interfaces = new List<Station_Interface>();
        private void Station_init()
        {
            //int a = 0;
            //station_Interfaces.Clear();
            //tableLayoutPanel1.Controls.Clear();
            //for (int i = 0; i < 2; i++)
            //{
            //    for (int h = 0; h < 4; h++)
            //    {
            //        Station_Interface buf = new Station_Interface(equipment_management1._DataTable.Rows[a][1].ToString(), equipment_management1._DataTable.Rows[a][2].ToString());
            //        Station_Interface._formlog = formlog;
            //        buf.Dock = DockStyle.Fill;
            //        buf._logs_Click += Buf__logs_Click;                 //工位模块logs打印事件
            //        buf._AlarmAdd_Click += Buf__AlarmAdd_Click;         //显示报警信息事件 
            //        buf._Alarmdelete_Click += Buf__Alarmdelete_Click;   //指定删除报警信息事件
            //        buf._BarPint_Click += Buf__BarPint_Click;           //条码打印事件
            //        buf._ProductNG_Click += Buf__ProductNG_Click;       //NG产品记录事件
            //        buf._Counterpoise_Click += Buf__Counterpoise_Click; //权限显示事件
            //        buf._BARstate_Click += Buf__BARstate_Click;
            //        tableLayoutPanel1.Controls.Add(buf, h, i);
            //        station_Interfaces.Add(buf);
            //        a++;
            //    }
            //}
        }

        //生产状态显示
        private void Buf__BARstate_Click(object sender)
        {
            Task _task = Task.Run(() => {

                try
                {
                    DataTable_buf = Program._mySql_Class.mySql_View("main_station_data_buf");
                    this.SafeInvoke(() =>
                    {
                        if (DataTable_buf != null)
                        {
                            userDataGridview2.Auto_sn = false;
                            GridviewShowDataTable2(DataTable_buf, userDataGridview2);
                        }
                    });
                }
                catch (Exception er)
                {
                    ;
                }

            });
        }

        private void Buf__Counterpoise_Click(object sender)
        {
            this.Invoke(new Action(() =>
            {
                try
                {
                    if (User_Lgon.User_Level != H_Level.None) materialLabel1.Text = "用户已登录   " + User_Lgon.User_LevelStr;
                    else materialLabel1.Text = "用户未登录";
                }
                catch {; }
            }));

        }

        //NG产品记录事件
        private void Buf__ProductNG_Click(object sender)
        {
            string[] buf = sender as string[];
            this.Invoke(new Action(() => {
                try
                {
                    //大于100条记录后清空
                    if (userDataGridview1.Rows.Count > 100)
                    {
                        userDataGridview1.Rows.Clear();
                    }
                    //添加信息
                    userDataGridview1.UserDataGridview_Add(buf[0], buf[1], buf[2]);
                }
                catch {; }
            }));
        }


        //条码打印事件
        private void Buf__BarPint_Click(object sender)
        {
            //ushort Copies = (ushort)sender;
            //printing_Interface1.Print_Hand = false;//设置为自动打印
            //int v_int = printing_Interface1.Print((int)Copies, true);
            //if (v_int == 0)
            //{
            //    if (printing_Interface1.Print_Image != null)
            //    {
            //        this.Invoke(new Action(() => {
            //            pictureBox2.Image = printing_Interface1.Print_Image;
            //        }));
            //    }
            //    main_LogicClass._Logs.Info("条码:" + printing_Interface1.Print_strint + "打印成功。。。  数量:"+ Copies.ToString());
            //}
            //else if (v_int == 2)
            //{
            //    this.Invoke(new Action(() => {
            //        pictureBox2.Image = getImage(Application.StartupPath + @"\Config\Print_null.JPG");
            //    }));
            //        main_LogicClass._Logs.Info("条码:" + printing_Interface1.Print_strint + "打印失败。。。信息：未发现打印设备！数量:" + Copies.ToString());
            //}
            //else0
            //{
            //    this.Invoke(new Action(() => { 
            //        pictureBox2.Image = getImage(Application.StartupPath + @"\Config\Print_null.JPG"); 
            //    }));
            //    main_LogicClass._Logs.Info("条码:" + printing_Interface1.Print_strint + "打印失败。。。信息：设备打印异常！ 数量:" + Copies.ToString());
            //}
        }


        //工位模块logs打印事件
        private void Buf__logs_Click(object sender)
        {
            Task _task = Task.Run(() => { Program._LogicClass.Info(sender.ToString());});
        }

        //报警信息更新事件
        private void alarmControl1__TableUpdate_Click(object sender, EventArgs e)
        {
            //更新报警信息到每个工位
            Update_Aliarm();
        }

        //指定报警信息更新报警解除时间事件
        private void Buf__Alarmdelete_Click(object sender)
        {
            Task _task = Task.Run(() =>
            {
                string[] buf_sender = sender as string[];
                if (buf_sender == null) return;

                string sql_str = "end_date='" + buf_sender[1] + "'";
                string[] sql_if = new string[] { "start_date", buf_sender[0], "alarm_id", buf_sender[3] };
                if(Program._mySql_Class.mySql_Update("alarm_data", sql_if, sql_str)==1)
                {
                    Program._LogicClass.Info("报警信息更新写入失败。 inif:" + sql_str);
                }


                this.SafeInvoke(() =>
                {
                    alarmControl1.AlarmInfo_delete(buf_sender[3]);
                });
            });
        }

        //显示报警信息事件 
        private void Buf__AlarmAdd_Click(object sender)
        {
            Task _task = Task.Run(() =>
            {
                string[] buf_sender = sender as string[];
                if (buf_sender == null) return;

                string sql_str = "(start_date,end_date,station_name,alarm_id,alarm_info)values('" +
                               buf_sender[0] + "','" + buf_sender[0] + "','" + buf_sender[1] + "','" + buf_sender[2] + "','" + buf_sender[4] + "')";
                if(Program._mySql_Class.mySql_add("alarm_data", sql_str)==1)
                {
                    Program._LogicClass.Info("报警信息写入失败。 inif:" + sql_str);
                }
                this.SafeInvoke(() =>
                {
                    string date = buf_sender[0].ToString();
                    string station = buf_sender[1].ToString();
                    string id = buf_sender[2].ToString();
                    string info = buf_sender[4].ToString();
                    alarmControl1.AlarmInfo_Add(date, station, id, info);
                });
            });
        }

        //任务
        bool Mtask_state = true;

        DataTable DataTable_buf;
        private void run_task()
        {
            int timer_count = 0;
            while (Mtask_state)
            {
                ;
            }
        }



        //筛选指定工位报警信息
        public DataTable getAlarmData(DataTable _dataTable, string Station_name)
        {
            DataTable newDt = new DataTable();//新建一个新的空DataTable
            newDt = _dataTable.Clone();
            newDt.TableName = Station_name;

            try
            {
                DataRow[] rows = _dataTable.Select("工位名称='"+Station_name+"'");//查询筛选
                if (rows!=null)
                {
                    foreach (DataRow row in rows)
                    { 
                        newDt.Rows.Add(row.ItemArray);//对新的dt赋值
                    }
                }

                return newDt;
            }
            catch (Exception er)
            {
                return null;
            }

        }

        //更新报警信息到每个工位
        public void Update_Aliarm()
        {
            Task _task = new Task(() =>
            {
                //获取每个工位的报警寄存器后更新到每个工位；
                if (alarmControl1.Alarm_dataTable != null && alarmControl1.Alarm_dataTable.Length > 0)
                {
                    for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
                    {
                        Station_Interface buf = tableLayoutPanel1.Controls[i] as Station_Interface;
                        if (buf != null)
                        {
                            //buf.Station.inAlarm_DataTable = alarmControl1.Alarm_dataTable[i];
                        }
                        ;
                    }
                }
            });
            _task.Start();
        }

        //退出界面
        private void End_form()
        {

            //退出逻辑
            Mtask_state = false;


            //_mySql_Class.mySql_Close();
            this.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Application.DoEvents();
            Application.ExitThread();
            Application.Exit();
            System.Environment.Exit(0);
        }

        //获取图片
        private Image getImage(string path)
        {
            Image Image_buf = null;
            try
            {
                if (!File.Exists(path))
                {
                    return null;
                }
                using (FileStream image = new FileStream(path, FileMode.Open))
                {
                    Image_buf = (Image)Image.FromStream(image, true).Clone();
                }
                return Image_buf;
            }
            catch (Exception er)
            {
                return null;
            }

        }


        //工位验证是否开启
 
        private void station_1_CheckedChanged(object sender, EventArgs e)
        {
 

        }
        private void station_1_MouseClick(object sender, MouseEventArgs e)
        {
            CheckBox buf = sender as CheckBox;
            if (buf != null)
            {
                int station_sn = 0;
                int.TryParse(buf.Text.Substring(1, 1), out station_sn);


                if (User_Lgon.User_Level == User_Lgon.H_Level.班组长 || User_Lgon.User_Level == User_Lgon.H_Level.管理员)
                {
                    if (buf.Checked)
                    {
                        buf.Text = "P" + station_sn + "_工位开启校验";
                        _ini_Class.Write_Ini("sys_data", buf.Name, buf.Checked.ToString());
                    }
                    else
                    {
                        buf.Text = "P" + station_sn + "_工位关闭校验";
                        _ini_Class.Write_Ini("sys_data", buf.Name, buf.Checked.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("权限错误，请确认权限", "错误提示");
                    bool check_buf = false;
                    bool.TryParse(_ini_Class.Read_Ini("sys_data", buf.Name), out check_buf);
                    buf.Checked = check_buf;
                    return;
                }



            }
        }

        //登录
        public Formlog formlog;
        private void MaterialButton1_Click(object sender, EventArgs e)
        {
            //string[] buf_sender = new string[5] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),"", "ADCE", "ADCE", "ADCE" };
            //string sql_str = "(start_date,end_date,station_name,alarm_id,alarm_info)values('" +
            //                   buf_sender[0] + "','" + buf_sender[0] + "','" + buf_sender[1] + "','" + buf_sender[2] + "','" + buf_sender[4] + "')";
            //Station_Interface.Table_mySql_Class.mySql_add("alarm_data", sql_str);

            ;
            logstate = true;
            formlog.ShowDialog();
            if (User_Lgon.User_Level != H_Level.None) materialLabel1.Text = "用户已登录   " + User_Lgon.User_LevelStr;
            else materialLabel1.Text = "用户未登录";
            logstate = false;
        }


        //打印
        private void MaterialButton2_Click(object sender, EventArgs e)
        {
            //if (User_Lgon.User_Level == User_Lgon.H_Level.生产员 || User_Lgon.User_Level == User_Lgon.H_Level.班组长 || User_Lgon.User_Level == User_Lgon.H_Level.管理员)
            //{
            //    if (station_Interfaces[0].StationPLC._PLC != null && station_Interfaces[7].StationPLC._IsConnected)
            //    {
            //        //获取打印数量 
            //        int _value = 0;
            //        int.TryParse(textBox1.Text, out _value);
            //        if (_value > 0)
            //        {
            //            Buf__BarPint_Click((ushort)_value);
            //        }
            //        else
            //        {
            //            MessageBox.Show("错误，打印数量小于等于0或输入异常！", "错误提示");
            //        }
            //    }
            //    else
            //    {
            //        MessageBox.Show("错误，PLC未连接或其他异常！", "错误提示");
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("权限错误，请确认权限", "错误提示");
            //    return;
            //}
        }


      
        public int GridviewShowDataTable2(DataTable table, DataGridView dataGridView)
        {
            try
            {
                dataGridView.Rows.Clear();
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        try
                        {
                            dataGridView.Rows.Add();
                            dataGridView.Rows[i].Cells[0].Value = i;
                            for (int j = 0; j < (table.Columns.Count - 1); j++)
                            {
                                dataGridView.Rows[i].Cells[0].Value = i;
                                dataGridView.Rows[i].Cells[j + 1].Value = table.Rows[i].ItemArray[j + 1].ToString();
                                Application.DoEvents();//可以交出控制权但是慢
                            }
                        }
                        catch {; }
                    }
                }

                return 0;
            }
            catch (Exception ex)
            { return 1; }

        }


    }
}
